home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: dd.chalmers.se!news.chalmers.se!sunic!psinntp!psinntp!chico.spr.com!psinntp!hns!jabba!hsspes1
- From: t_akrishnan@bart.hns.com
- Subject: Re: Survey: how does your compiler right-shift
- Message-ID: <28FEB199412014462@jabba>
- Sender: hsspes1@jabba (PES GROUP AT HSS)
- Date: Mon, 28 Feb 1994 17:01:00 GMT
- News-Software: VAX/VMS VNEWS 1.41
- References: <2kghb7$p53@nack.craycos.com> <CLxuHM.78n@bfsec.bt.co.uk>
- Organization: Hughes Network Systems, Inc.
- Lines: 47
-
- In article <CLxuHM.78n@bfsec.bt.co.uk>, dallison@bfsec.bt.co.uk writes...
- >In article p53@nack.craycos.com, ferguson@craycos.com (Scott Ferguson) writes:
- >>Right-shifting a signed int produces different results under different
- >>compilers, some fill in the upper bits with the sign bit, others just
- >>fill with zeros. I'm porting some code that relies heavily on the assumption
- >>that signed int's get filled with the sign bit (it's the mpeg encoder and
- >>decoder).
-
- >Surely the whole idea of a signed shift right is to sign extend. Is this not
- >defined in the ANSI standard?
-
- The result of E1>>E2 is E1 right shifted E2 bit positions. In addition,
- if E1 is an unsigned type or a signed type with a non-negative value,
- E1>>E2 is defined to yield the result E1 / (2 ^ E2) - where the ^ represents
- exponentiation. The reason is presumably to ensure that 0>>E2 would always
- yield 0. On a ones complement machine, an all-bit set value represents a sort
- of 0, in this case, the >> operator will have to do a SIGNED SHIFT to
- generate an all-bit-set zero as the result.
-
- On a twos complement machine, the right shift operator for a
- non-negative value is always a bit-shift, with 0 filling from the left. Thus
- it may or may not be a signed shift - the effects are equivalent.
-
- For negative values of E1 (which implies a signed type for E1),
- the result is implementation-defined. This would allow systems with no
- signed shift instruction to implement the >> operator efficiently. Ones
- complement machines with no signed-shift are the only category which would
- have to go to extra lengths to implement >>.
-
- >Most machines have instruction modifiers for
- >both types of shift.
-
- But some don't have. The ANSI Rationale 3.3.7 (on a signed shift
- requirement):
- " ... such a requirement might slow down fast code and ... the
- usefulness of sign extended shifts is marginal. (Shifting a negative
- twos-complement integer arithmetically right one place is NOT the same as
- dividing by two!)."
-
- -----
- /* What is mind? No matter.
- What is matter? Never mind. */
-
- /* Ajoy Krishnan T,
- Senior S/W Engineer, Hughes Software Systems,
- New Delhi - 19, India.
- (ajoyk%hss@lando.hns.com)*/
-